﻿using System;
using System.Collections;
using System.Collections.Generic;
using TrueSync;
using Framework;

public class DisappearReasons
{
    public bool toggle;
    public List<int> CallbackEventIDAfterDisappear;
    public void Serialize(EByte ms)
    {
        ms.WriteInt(toggle ? 1 : 0);
        ms.WriteInt(CallbackEventIDAfterDisappear.Count);
        for (int i = 0; i < CallbackEventIDAfterDisappear.Count; i++)
        {
            ms.WriteInt(CallbackEventIDAfterDisappear[i]);
        }
    
    }

    public void Deserialize(EByte ms)
    {

        toggle = ms.ReadInt() == 1;
        int CallbackEventLen = ms.ReadInt();
        CallbackEventIDAfterDisappear = new List<int>(CallbackEventLen);
        for (int i = 0; i < CallbackEventLen; i++)
        {
            CallbackEventIDAfterDisappear.Add(ms.ReadInt());
        }
    }
}
public enum WeaponEnum
{
    Weapon1,
    Weapon2
}
public  enum ParticleSubitemInitPositionEnum
{
    OnMyEntity,
    OnRivalsEntity,
    OnTargetPosition,
    OnUseParentHangpoint
}
public class OnUserParentHangpointDef
{
   public int HangpointID;
   public bool Synchronization;
    public  void Serialize(EByte ms)
    {
        ms.WriteInt(HangpointID);
        ms.WriteInt(Synchronization?1:0);
    }

    public  void Deserialize(EByte ms)
    {
        HangpointID = ms.ReadInt();
        Synchronization =ms.ReadInt()==1;
    }
}

public enum HangpointBelongToPartEnum
{
    Body,
    Weapon1,
    Weapon2,
    HiddenWeapon1,
    HiddenWeapon2
}


public class HangpointDef
{
    public int HangpointID;
    public HangpointBelongToPartEnum HangpointBelongToPart;

    public virtual void Serialize(EByte ms)
    {
        ms.WriteInt(HangpointID);
        ms.WriteInt((int)HangpointBelongToPart);
    }

    public virtual void Deserialize(EByte ms)
    {
        HangpointID = ms.ReadInt();
        HangpointBelongToPart = (HangpointBelongToPartEnum)ms.ReadInt();
    }
}

public enum SubitemTypeEnum
{
    StandardSubitem,
    FlySubitem,
}

public enum SubitemModelTypeEnum
{
    Particle,
    ShadowOnCasterAll,
    ShadowOnCasterWeapon1,
    ShadowOnCasterWeapon2,
    ShadowOnParentModel,
    ShadowOnTargetAll,
}

public struct ParticleSubitemDesc
{
    public ParticleSubitemInitPositionEnum ParticleSubitemInitPosition;
    public OnUserParentHangpointDef OnUserParentHangpoint;
    public int ParticleID;
    public float ParticleScalePercent;
    public bool UseParticleHangpoint2;
    public HangpointDef ParticleHangpoint2Def;
    public bool UseLight;
    public int LightID;

    public void Serialize(EByte ms)
    {
        ms.WriteInt((int)ParticleSubitemInitPosition);
        OnUserParentHangpoint.Serialize(ms);
        ms.WriteInt(ParticleID);
        ms.WriteFloat(ParticleScalePercent);
        ms.WriteInt(UseParticleHangpoint2 ? 1 : 0);
        ParticleHangpoint2Def.Serialize(ms);
        ms.WriteInt(UseLight ? 1 : 0);
        ms.WriteInt(LightID);
    }

    public void Deserialize(EByte ms)
    {
        ParticleSubitemInitPosition = (ParticleSubitemInitPositionEnum)ms.ReadInt();
        OnUserParentHangpoint.Deserialize(ms);
        ParticleID = ms.ReadInt();
        ParticleScalePercent = ms.ReadFloat();
        UseParticleHangpoint2 = ms.ReadInt() == 1;
        ParticleHangpoint2Def = new HangpointDef();
        ParticleHangpoint2Def.Deserialize(ms);
        UseLight = ms.ReadInt() == 1;
        LightID = ms.ReadInt();
    }
}

public class ShadowWithWeaponSubitemDesc : ShadowSubitemDesc
{
    public WeaponEnum Weapon;

    public override void Serialize(EByte ms)
    {
        base.Serialize(ms);
        ms.WriteInt((int)Weapon);
    }

    public override void Deserialize(EByte ms)
    {
        base.Deserialize(ms);
        Weapon = (WeaponEnum)ms.ReadInt();
    }
}
public class ShadowSubitemDesc
{
    public float Transparency;
    public float AngleOffset;
    public string AnimationName;

    public virtual void Serialize(EByte ms)
    {
        ms.WriteFloat(Transparency);
        ms.WriteFloat(AngleOffset);
        ms.WriteString(AnimationName);
    }

    public virtual void Deserialize(EByte ms)
    {
        Transparency = ms.ReadFloat();
        AngleOffset = ms.ReadFloat();
        AnimationName = ms.ReadString();
    }
}

public enum SubitemTotalFrameTypeEnum
{
    Normal,
    Charge,
}

public enum SubitemDisappearReasonEnum
{
    HitOtherFlySubitem,
    HitTarget,
    Reachtarget,
    SkillStreamFinish,
    SkillGroupFinish,
    HitGround,
    CasterDie,
    TargetDie,
    Callback,
    SubitemDisappearReasonNum,
}

[System.Serializable]
public class LogicSubItemPropertiesComponent : LogicComponent
{
    public List<DisappearReasons> DisappearReasonsList;
    public ShadowWithWeaponSubitemDesc ShadowWithWeaponSubitem;
    LogicEntity mLEntity;
    public string SubitemName;
    public int SubitemID;
    public SubitemTypeEnum SubitemType;
    public FP YPositionOffset;
    public SubitemModelTypeEnum SubitemModelType;
    public ParticleSubitemDesc ParticleSubitem;
    public ShadowSubitemDesc ShadowSubitem;
    public SubitemTotalFrameTypeEnum SubitemTotalFrameType;
    public int TotalFramesNum;
    public int MultipleChargeTime;
    public int DisappearFramesNum;
    public List<bool> DisappearReasons;
    public List<int> CallbackEventIDBeforeDisappear;

    List<int> mEventIDs;

    int mFrames;

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
    }

    public void Start()
    {
        //注册触发器
        LogicEventComponent eventComponent = mLEntity.GetLogicComponent<LogicEventComponent>();
        eventComponent.SetEvent(mEventIDs);
    }

   
    public void DoUpdate(FP deltaTime)
    {

    }

    public void Destroy()
    {
        LogicEventComponent eventComponent = mLEntity.GetLogicComponent<LogicEventComponent>();
        for(int i = 0; i < CallbackEventIDBeforeDisappear.Count; i++)
        {
            eventComponent.CallEvent(CallbackEventIDBeforeDisappear[i]);
        }
        eventComponent.DestroyEvent(mEventIDs);
    }

    public void Serialize(EByte ms)
    {
        ms.WriteInt(DisappearReasonsList.Count);
        for (int i = 0; i < DisappearReasonsList.Count; i++)
        {
            DisappearReasonsList[i].Serialize(ms);
        }
        ShadowWithWeaponSubitem.Serialize(ms);
        ms.WriteString(SubitemName);
        ms.WriteInt(SubitemID);
        ms.WriteInt((int)SubitemType);
        if(SubitemType == SubitemTypeEnum.StandardSubitem)
        {
            mLEntity.GetLogicComponent<LogicStandardSubItemPositionComponent>().Serialize(ms);
        }
        else if(SubitemType == SubitemTypeEnum.FlySubitem)
        {
            mLEntity.GetLogicComponent<LogicFlySubItemMoveComponent>().Serialize(ms);
        }

        ms.WriteLong(YPositionOffset.RawValue);

        ms.WriteInt((int)SubitemModelType);
        if(SubitemModelType == SubitemModelTypeEnum.Particle)
        {
            ParticleSubitem.Serialize(ms);
        }
        else
        {
            ShadowSubitem.Serialize(ms);
        }

        ms.WriteInt((int)SubitemTotalFrameType);
        if(SubitemTotalFrameType == SubitemTotalFrameTypeEnum.Normal)
        {
            ms.WriteInt(TotalFramesNum);
        }
        else
        {
            ms.WriteInt(MultipleChargeTime);
        }
        ms.WriteInt(DisappearReasons.Count);
        for(int i = 0; i < DisappearReasons.Count; i++)
        {
            ms.WriteInt(DisappearReasons[i] ? 1 : 0);
        }

        ms.WriteInt(CallbackEventIDBeforeDisappear.Count);
        for(int i = 0; i < CallbackEventIDBeforeDisappear.Count; i++)
        {
            ms.WriteInt(CallbackEventIDBeforeDisappear[i]);
        }

    }

    public void Deserialize(EByte ms)
    {
        int disappearReasonsCount = ms.ReadInt();
        DisappearReasonsList = new List<DisappearReasons>(disappearReasonsCount);
        for (int i = 0; i < DisappearReasonsList.Count; i++)
        {
            DisappearReasonsList.Add(new DisappearReasons());
            DisappearReasonsList[i].Deserialize(ms);
        }
        ShadowWithWeaponSubitem = new ShadowWithWeaponSubitemDesc();
        ShadowWithWeaponSubitem.Deserialize(ms);
        SubitemName = ms.ReadString();
        SubitemID = ms.ReadInt();
        SubitemType = (SubitemTypeEnum)ms.ReadInt();
        if(SubitemType == SubitemTypeEnum.StandardSubitem)
        {
            mLEntity.GetLogicComponent<LogicStandardSubItemPositionComponent>().Deserialize(ms);
        }
        else
        {
            mLEntity.GetLogicComponent<LogicFlySubItemMoveComponent>().Deserialize(ms);
        }

        YPositionOffset = FP.FromRaw(ms.ReadLong());

        SubitemModelType = (SubitemModelTypeEnum)ms.ReadInt();
        if(SubitemModelType == SubitemModelTypeEnum.Particle)
        {
            ParticleSubitem = new ParticleSubitemDesc();
            ParticleSubitem.Deserialize(ms);
        }
        else
        {
            ShadowSubitem = new ShadowSubitemDesc();
            ShadowSubitem.Deserialize(ms);
        }

        SubitemTotalFrameType = (SubitemTotalFrameTypeEnum)ms.ReadInt();
        if(SubitemTotalFrameType == SubitemTotalFrameTypeEnum.Normal)
        {
            TotalFramesNum = ms.ReadInt();
        }
        else
        {
            MultipleChargeTime = ms.ReadInt();
        }

        DisappearReasons = new List<bool>(ms.ReadInt());
        for(int i = 0; i < DisappearReasons.Count; i++)
        {
            DisappearReasons[i] = ms.ReadInt() == 1;
        }

        CallbackEventIDBeforeDisappear = new List<int>(ms.ReadInt());
        for(int i = 0; i < CallbackEventIDBeforeDisappear.Count; i++)
        {
            CallbackEventIDBeforeDisappear[i] = ms.ReadInt();
        }
    }
}
